﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DVSProject.Models;

namespace DVSProject.Lib
{
    public class TitleMechanicalRepo
    {
        public const string m_msg_head = "Title Mechanical";

        public IEnumerable<TitleMechanicalModels> GetTitleMechanical()
        {
            List<TitleMechanicalModels> TitleMechanicals = new List<TitleMechanicalModels>();
            string SQLCommand = @"select * from TBTitleMechanical WHERE Status <> 'Delete' Order by TitleMechanicalType,TitleMechanicalId";
            using (var db = new DBHelper())
            {
                try
                {
                    var reader = db.ExecuteReader(SQLCommand);
                    while (reader.Read())
                    {
                        TitleMechanicalModels TitleMechanical = new TitleMechanicalModels();
                        TitleMechanical.TitleMechanicalId = (int)reader["TitleMechanicalId"];
                        TitleMechanical.TitleMechanicalType = (string)reader["TitleMechanicalType"];
                        TitleMechanical.TitleMechanicalName = (string)reader["TitleMechanicalName"];
                        TitleMechanical.Status = (string)reader["Status"];

                        TitleMechanicals.Add(TitleMechanical);
                    }
                }
                catch (Exception ex)
                {
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0001", ex));
                }
            }
            return TitleMechanicals.ToList();
        }

        public IEnumerable<TitleMechanicalModels> GetTitleMechanicalDropDown()
        {
            List<TitleMechanicalModels> TitleMechanicals = new List<TitleMechanicalModels>();
            string SQLCommand = @"SELECT distinct TitleMechanicalType FROM TBTitleMechanical WHERE Status <> 'Delete'";
            using (var db = new DBHelper())
            {
                try
                {
                    var reader = db.ExecuteReader(SQLCommand);
                    while (reader.Read())
                    {
                        TitleMechanicalModels TitleMechanical = new TitleMechanicalModels();
                        TitleMechanical.TitleMechanicalType = (string)reader["TitleMechanicalType"];

                        TitleMechanicals.Add(TitleMechanical);
                    }
                }
                catch (Exception ex)
                {
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0001", ex));
                }
            }
            return TitleMechanicals.ToList();
        }

        public TitleMechanicalModels GetTitleMechanicalById(int id)
        {
            TitleMechanicalModels TitleMechanical = new TitleMechanicalModels();
            string SQLCommand = @"select * from TBTitleMechanical where TitleMechanicalId=@TitleMechanicalId";

            using (var db = new DBHelper())
            {
                try
                {
                    db.AddParameter("@TitleMechanicalId", id);
                    var reader = db.ExecuteReader(SQLCommand);
                    if (reader.Read())
                    {
                        TitleMechanical.TitleMechanicalId = (int)reader["TitleMechanicalId"];
                        TitleMechanical.TitleMechanicalType = (string)reader["TitleMechanicalType"];
                        TitleMechanical.TitleMechanicalName = (string)reader["TitleMechanicalName"];
                        TitleMechanical.Status = (string)reader["Status"];

                    }
                }
                catch (Exception ex)
                {
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0002", ex));
                }
            }
            return TitleMechanical;
        }

        public bool Create(TitleMechanicalModels TitleMechanical)
        {
            bool result = true;
            string SQLCommand = @"INSERT into TBTitleMechanical
                                    values (@TitleMechanicalType,@TitleMechanicalName,@Status)";

            using (var db = new DBHelper())
            {
                try
                {
                    db.BeginTransaction();
                    db.AddParameter("@TitleMechanicalType", TitleMechanical.TitleMechanicalType);
                    db.AddParameter("@TitleMechanicalName", TitleMechanical.TitleMechanicalName);
                    db.AddParameter("@Status", "Active");

                    db.ExecuteNonQuery(SQLCommand, DbConnectionState.KeepOpen);
                    db.CommitTransaction();
                }
                catch (Exception ex)
                {
                    db.RollbackTransaction();
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0003", ex));
                    return false;
                }
            }
            return result;
        }

        public bool Edit(TitleMechanicalModels TitleMechanical)
        {
            bool result = true;
            string SQLCommand = @"Update TBTitleMechanical 
                                    Set TitleMechanicalType=@TitleMechanicalType,
                                        TitleMechanicalName=@TitleMechanicalName
                                    where TitleMechanicalId=@TitleMechanicalId";

            using (var db = new DBHelper())
            {
                try
                {
                    db.BeginTransaction();
                    db.AddParameter("@TitleMechanicalId", TitleMechanical.TitleMechanicalId);
                    db.AddParameter("@TitleMechanicalType", TitleMechanical.TitleMechanicalType);
                    db.AddParameter("@TitleMechanicalName", TitleMechanical.TitleMechanicalName);

                    db.ExecuteNonQuery(SQLCommand, DbConnectionState.KeepOpen);
                    db.CommitTransaction();
                }
                catch (Exception ex)
                {
                    db.RollbackTransaction();
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0004", ex));
                    return false;
                }
            }
            return result;
        }

        public bool Delete(TitleMechanicalModels TitleMechanical)
        {
            bool result = true;
            string SQLCommand = @"Update TBTitleMechanical
                                    Set Status=@Status
                                    where TitleMechanicalId=@TitleMechanicalId";

            using (var db = new DBHelper())
            {
                try
                {
                    db.BeginTransaction();
                    db.AddParameter("@TitleMechanicalId", TitleMechanical.TitleMechanicalId);
                    db.AddParameter("@Status", "Delete");

                    db.ExecuteNonQuery(SQLCommand, DbConnectionState.KeepOpen);
                    db.CommitTransaction();
                }
                catch (Exception ex)
                {
                    db.RollbackTransaction();
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0005", ex));
                    return false;
                }
            }
            return result;
        }
    }
}